package mspbots.timedoctor.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * mspbots.data.cw.utils.FilterExpression
 * {
 * "op": "eq",
 * "field": "IsActive",
 * "value": true
 * },
 * {
 * "op": "or",
 * "items": [
 * {
 * "op": "eq",
 * "field": "lastname",
 * "value": "Smith"
 * },
 * {
 * "op": "eq",
 * "field": "lastname",
 * "value": "Jones"
 * },
 * {
 * "op": "and",
 * "items": [
 * {
 * "op": "eq",
 * "field": "firstname",
 * "value": "Chris"
 * },
 * {
 * "op": "eq",
 * "field": "lastname",
 * "value": "Brady"
 * }
 * ]
 * }
 * ]
 * }
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/7/10
 */
@Data
@NoArgsConstructor
@ApiModel("Auto task api query filter expression model")
public class FilterExpression {

    /**
     * Operator     Definition <br>
     * eq:	        Requires that the field value match the exact criteria provided <br>
     * noteq	    Requires that the field value be anything other than the criteria provided <br>
     * gt/gte	    Requires that the field value be greater than or equal to the criteria provided <br>
     * lt/lte	    Requires that the field value be less than or equal to the criteria provided <br>
     * beginsWith	Requires that the field value begin with the defined criteria <br>
     * endsWith 	Requires that the field value end with the defined criteria <br>
     * contains	    Allows for the string provided as criteria to match any resource that contains the string in its value <br>
     * exist	    Enter exist to query for fields in which the data you specify is not null. <br>
     * notExist     Enter notExist to query for fields in which the specified data is null. <br>
     * in	        With this value specified, the query will return only the values in the list array that match the field value you specify. <br>
     * notIn	    With this value specified, the query will only return the values in the list array that do not match the field value you specify. <br>
     */
    @NotBlank(message = "Filter operators  [op] can not empty," +
            "value is [eq,noteq,gt/gte,lt/lte,beginsWith,endsWith,contains,exist,notExist,in,notIn]")
    @ApiModelProperty(example = "gte")
    private String op;

    @NotBlank(message = "Filter operators  [field] can not empty,")
    @ApiModelProperty(example = "id")
    private String field;

    @NotBlank(message = "Filter operators  [value] can not empty,")
    @ApiModelProperty(example = "0")
    private String value;

    @ApiModelProperty(value = "Sub filter expression")
    private List<FilterExpression> items;

    @Builder
    public FilterExpression(String op, String field, String value, List<FilterExpression> items) {
        this.op = op;
        this.field = field;
        this.value = value;
        this.items = items;
    }

    public static FilterExpression withDefault() {
        return new FilterExpression("gte", "id", "0", null);
    }

}
